package com.java.controller;

import com.java.pojo.Student_info;
import com.java.pojo.Teacher_info;
import com.java.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.Map;

/**
 * description:描述
 * author:作者
 * time:2020/11/22 23:31
 */
@Controller
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    /**
     * 注册添加教师信息
     * @param teacherInfo
     * @return
     */
    @RequestMapping("/setTeacherInfo.do")
    public @ResponseBody boolean setTeacherInfo( @RequestBody Map<String,Object> teacherInfo){
        Teacher_info teacher_info = new Teacher_info();
        teacher_info.setCode((String) teacherInfo.get("code"));
        teacher_info.setTeacherWxid((String) teacherInfo.get("teacherWxid"));
        teacher_info.setTeacherName((String) teacherInfo.get("teacherName"));
        teacher_info.setPassword((String) teacherInfo.get("password"));
        System.out.println(teacher_info.toString());
        return registerService.addTeacherInfo(teacher_info);
    }

    /**
     * 注册插入学生的信息
     * @param studentInfo
     * @return
     */
    @RequestMapping("/setStudentInfo.do")
    public @ResponseBody boolean setStudentInfo(@RequestBody Map<String,Object> studentInfo) throws Exception{
        Student_info student_info = new Student_info();
        student_info.setImage_path((String) studentInfo.get("image_path"));
        student_info.setSno((String) studentInfo.get("sno"));
        student_info.setStudentClass((String) studentInfo.get("studentClass"));
        String studentName = (String) studentInfo.get("studentName");
        //byte[] bytes = studentName.getBytes("iso-8859-1");
        //String stuName = new String(bytes,"utf-8");
        student_info.setStudentName((String) studentInfo.get("studentName"));
        //student_info.setStudentName(stuName);
        student_info.setStudentWxid((String) studentInfo.get("studentWxid"));

        System.out.println(student_info.getStudentName());
        return registerService.addStudentInfo(student_info);
    }


    /**
     * 登录微信
     * @param code
     * @return
     */
    @RequestMapping("/loginWx.do")
    public @ResponseBody String loginWx(String code){
        System.out.println("codeSession:"+code);
        String s=httpRequest("https://api.weixin.qq.com/sns/jscode2session?appid=wxc589cab1859a28d5&secret=c38b5ec7c80794344da21863542d23cf&js_code="+code+"&grant_type=authorization_code","GET","");
        System.out.println(s);
        return s;
    }
    /**
     * 获取地理位置
     * @param code
     * @return
     */
    @RequestMapping("/loginWx.do")
    public @ResponseBody String loginWxGIS(String code){

        String gis=httpRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc589cab1859a28d5&secret=c38b5ec7c80794344da21863542d23cf","GET","");
        System.out.println(gis);
        return gis;
    }

    /*
     * 处理https GET/POST请求
     * 请求地址、请求方法、参数
     * */
    private String httpRequest(String requestUrl,String requestMethod,String outputStr) {
        StringBuffer buffer=null;
        try{
            //创建SSLContext
            /*SSLContext sslContext=SSLContext.getInstance("SSL");
            TrustManager[] tm={new X509TrustManager()};
            //初始化
            sslContext.init(null, tm, new java.security.SecureRandom());;
            //获取SSLSocketFactory对象
            SSLSocketFactory ssf=sslContext.getSocketFactory();*/
            URL url=new URL(requestUrl);
            HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod(requestMethod);
            //设置当前实例使用的SSLSoctetFactory
            //   conn.setSSLSocketFactory(ssf);
            conn.connect();
            //往服务器端写内容
            if(null!=outputStr){
                OutputStream os=conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }

            //读取服务器端返回的内容
            InputStream is=conn.getInputStream();
            InputStreamReader isr=new InputStreamReader(is,"utf-8");
            BufferedReader br=new BufferedReader(isr);
            buffer=new StringBuffer();
            String line=null;
            while((line=br.readLine())!=null){
                buffer.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return buffer.toString();
    }





}
